package org.example.flea_market_mvc.service.products.impl;

import org.example.flea_market_mvc.entity.PageInfo;
import org.example.flea_market_mvc.entity.Products;
import org.example.flea_market_mvc.mapper.ProductsMapper;
import org.example.flea_market_mvc.param.ProductParam;
import org.example.flea_market_mvc.service.products.ProductsService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商品服务实现类
 * 处理商品上架、下架、库存管理等核心业务逻辑
 */
@Service
public class ProductsServiceImpl implements ProductsService {

    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public int insert(ProductParam productParam) {
        Products product = new Products();
        BeanUtils.copyProperties(productParam, product);
        return productsMapper.insert(product);
    }

    @Override
    public int deleteById(Integer id) {
        return productsMapper.deleteById(id);
    }

    @Override
    public int update(Products productParam) {

        return productsMapper.update(productParam);
    }

    @Override
    public Products selectById(Integer id) {
        return productsMapper.selectById(id);
    }

    @Override
    public List<Products> selectAll() {
        return productsMapper.selectAll();
    }

    @Override
    public PageInfo<Products> selectByPage(int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Products> products = productsMapper.selectByPage(offset, pageSize);
        int total = productsMapper.selectCount();
        return new PageInfo<>(pageNum, pageSize, total, products);
    }

    @Override
    public PageInfo<Products> selectByCategory(int categoryId, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<Products> products = productsMapper.selectByCategory(categoryId, offset, pageSize);
        int total = productsMapper.selectCountByCategory(categoryId);
        return new PageInfo<>(pageNum, pageSize, total, products);
    }

    @Override
    public PageInfo<Products> selectPage(ProductParam param) {
        int pageNum = param.getPageNum() == null ? 1 : param.getPageNum();
        int pageSize = param.getPageSize() == null ? 10 : param.getPageSize();
        int offset = (pageNum - 1) * pageSize;
        List<Products> products = productsMapper.selectPageByParam(param, offset, pageSize);
        int total = productsMapper.countByParam(param);
        return new PageInfo<>(pageNum, pageSize, total, products);
    }

    @Override
    public int upProduct(Integer id) {
        return productsMapper.updateStatus(id, 1); // 1=上架
    }

    @Override
    public int downProduct(Integer id) {
        return productsMapper.updateStatus(id, 2); // 2=下架
    }

    @Override
    public int auditProduct(Integer id, Integer status) {
        return productsMapper.updateStatus(id, status); // status=审核状态
    }
    @Override
    public int upDataProductStatus(Integer id, Integer status){
        return productsMapper.updateStatus(id, status);
    }

    @Override
    public int forceDownProduct(Integer id) {
        return productsMapper.updateStatus(id, 3); // 3=强制下架
    }
}